package com.zut.questionnaire.service;

import com.zut.questionnaire.pojo.Answer;
import com.zut.questionnaire.pojo.AnswerSheet;
import com.zut.questionnaire.pojo.Result;
import com.zut.questionnaire.repository.AnswerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Optional;

@Service
@Transactional
public class AnswerService {

    @Autowired
    private AnswerRepository answerRepository;

    public Result findById(String id){

        Optional<Answer> optionalAnswer = answerRepository.findById(id);

        Answer answer=null;

        if(optionalAnswer.isPresent()){
            answer = optionalAnswer.get();

            return Result.OK(answer);
        }
        return Result.ERROR(500,"查询id不存在");
    }

    public Result save(Answer answer){

        answerRepository.save(answer);

        return Result.OK();
    }

    public Result update(String id,Answer answer){
        answer.setId(id);
        answerRepository.save(answer);

        return Result.OK();
    }

    public Result delete(String id){

        answerRepository.deleteById(id);

        return Result.OK();
    }

}
